$(function() {
    YearListLoad();
    AddMoneyValue();
    OutMoneyValue();
    YearMMoneyValue();
    MoneyYearAdd();

});

function YearListLoad() {
    var userid = localStorage.user_id || sessionStorage.user_id;
    var token = localStorage.token || sessionStorage.token;
    $.ajax({
            url: host + "/money_list/" + userid,
            method: "get",
            // data: JSON.stringify(params),
            headers: {'Authorization': 'JWT ' + token},
            contentType: "application/json",
            xhrFields: {
                withCredentials: true
            },
            success: function (resp) {
                var list = eval(resp);
                html = ''
                var all_money = 0;
                var all_profit = 0;
                for (var i in list){
                    all_money = all_money + Number(list[i].dn_year_add)
                    all_profit = all_profit + Number(list[i].dn_year_profit)
                    // console.log(all_money_c)
                    html +=
                        "<tr>"+
                        "<td class='money_table'>" + list[i].year + "</td>"+
                        '<td class="money_table">' + list[i].year_c_money + "</td>"+
                        '<td class="money_table">' + list[i].year_z_add + "</td>"+
                        '<td class="money_table">' + list[i].year_z_out + "</td>"+
                        '<td class="money_table">' + list[i].dn_year_add + "</td>"+
                        '<td class="money_table">' + list[i].dn_year_money + "</td>"+
                        '<td class="money_table">' + list[i].year_m_money + "</td>"+
                        '<td class="money_table">' + list[i].dn_year_profit + "</td>"+
                        '<td class="money_table">'+
                            '<div class="btn-group" style="margin-top: -15px; max-height: 20px">'+
                              '<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">'+
                                '操作 <span class="caret"></span>'+
                              '</button>'+
                              '<ul class="dropdown-menu">'+
                                '<li><a href="#" data-toggle="modal" data-target=".add_money" onclick=values(' + list[i].id + ')>添加资金</a></li>'+
                                '<li><a href="#" data-toggle="modal" data-target=".out_money" onclick=values(' + list[i].id + ')>取出资金</a></li>'+
                                '<li><a href="#" data-toggle="modal" data-target=".year_m_money" onclick=values(' + list[i].id + ')>添加总资金</a></li>'+
                                '<li><a href="#" onclick="YearDelete(this)" id=' + list[i].id + '>删除</a></li>'+
                              '</ul>'+
                            '</div>'+
                        '</td>'+
                    '</tr>'
                }
                $("#all_money").html(Number(all_money.toFixed(4)) + Number(list[0].year_c_money));    //总本金
                $("#all_profit").html(Number(all_profit.toFixed(4)));   //总收益
                html1 =
                    '<tr>'+
                        '<th class="money_table">年份</th>'+
                        '<th class="money_table">年初资金(万元)</th>'+
                        '<th class="money_table">年中追加资金(万元)</th>'+
                        '<th class="money_table">年中取出资金(万元)</th>'+
                        '<th class="money_table">当年追加本金(万元)</th>'+
                        '<th class="money_table" style="color: red">当年本金(万元)</th>'+
                        '<th class="money_table" style="color: red">总资金(万元)</th>'+
                        '<th class="money_table" style="color: red">当年盈利(万元)</th>'+
                        '<th class="money_table">操作</th>'+
                    '</tr>'
                 $('#my_article_list').html(html1 + html)
            },
            error: function (resp) {
                alert('服务器异常,请重试！')
            }
        })
}


// 删除年份
function YearDelete(e) {
    var token = localStorage.token || sessionStorage.token;
    var year_id = $(e).attr('id');
    var result = confirm('确定删除?');
    // console.log(year_id)
    if (result){
        $.ajax({
            url: host + "/money_list_del/" + year_id ,
            method: "delete",
            // data: params,
            headers: {'Authorization': 'JWT ' + token},
            contentType: "application/json",
            xhrFields: {
                withCredentials: true
            },
            success: function (resp) {
                $(e).parent().parent().parent().parent().parent().remove()
                window.location.reload();
            },
            error: function (resp) {
                alert('服务器异常,请重试！')
            }
        })
    }
}

//增加资金
function AddMoneyValue() {
    $("#add_money_but").click(function () {
        var money_value = $("#add_money_value").val();
        var token = localStorage.token || sessionStorage.token;
        var year_id = $('.year_m_money_id').val();
        // console.log(money_value, year_id);
        params = {
            'year_z_add': money_value,
        }
        $.ajax({
            url: host + "/add_money/" + year_id,
            method: "put",
            data: JSON.stringify(params),
            headers: {'Authorization': 'JWT ' + token},
            contentType: "application/json",
            xhrFields: {
                withCredentials: true
            },
            success: function (resp) {
                window.location.reload();

            },
            error: function (resp) {
                alert('服务器异常,请重试！')
            }
        })

    })
}


//取出资金
function OutMoneyValue() {
    $("#out_money_but").click(function () {
        var money_value = $("#out_money_value").val();
        var token = localStorage.token || sessionStorage.token;
        var year_id = $('.year_m_money_id').val();
        // console.log(money_value, year_id);
        params = {
            'year_z_out': money_value,
        }
        $.ajax({
            url: host + "/out_money/" + year_id,
            method: "put",
            data: JSON.stringify(params),
            headers: {'Authorization': 'JWT ' + token},
            contentType: "application/json",
            xhrFields: {
                withCredentials: true
            },
            success: function (resp) {
                window.location.reload();

            },
            error: function (resp) {
                alert('服务器异常,请重试！')
            }
        })

    })
}


//添加年末资金
function YearMMoneyValue() {
    $("#year_m_but").click(function () {
        var money_value = $("#year_m_value").val();
        var token = localStorage.token || sessionStorage.token;
        var year_id = $('.year_m_money_id').val();
        // console.log(money_value, year_id);
        params = {
            'year_m_money': money_value,
        }
        $.ajax({
            url: host + "/year_m_money/" + year_id,
            method: "put",
            data: JSON.stringify(params),
            headers: {'Authorization': 'JWT ' + token},
            contentType: "application/json",
            xhrFields: {
                withCredentials: true
            },
            success: function (resp) {
                window.location.reload();

            },
            error: function (resp) {
                alert('服务器异常,请重试！')
            }
        })

    })
}

// 往模态框里传值
function values(ID) {
    $('.year_m_money_id').val(ID);
}


//添加年份 和年初资金
function MoneyYearAdd() {
    $('#year_c_money_but').click(function () {
        var year = $('#year_c_add').val();
        var money = $('#year_c_money').val();
        var token = localStorage.token || sessionStorage.token;
        // console.log(year,money)
        params = {
            'year':year,
            'year_c_money':money

        }
        $.ajax({
            url: host + "/money_year_add/" ,
            method: "post",
            data: JSON.stringify(params),
            headers: {'Authorization': 'JWT ' + token},
            contentType: "application/json",
            xhrFields: {
                withCredentials: true
            },
            success: function (resp) {
                location.href = './money.html';
            },
            error: function (resp) {
                alert('服务器异常,请重试！')
            }
        })




    })
}